#region File Description
//-----------------------------------------------------------------------------
// This a modified version of the InputManager class in the XNA role playing game kit.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
#endregion

namespace Magnet
{
    /// <summary>
    /// This class handles all keyboard and gamepad actions in the game.
    /// </summary>
    public static class InputManager
    {
        public const Keys UP = Keys.Up;
        public const Keys DOWN = Keys.Down;
        public const Keys LEFT = Keys.Left;
        public const Keys RIGHT = Keys.Right;
        public const Keys OK = Keys.Enter;
        public const Keys BACK = Keys.Escape;

        public const Keys A = Keys.A;
        public const Keys W = Keys.W;
        public const Keys S = Keys.S;
        public const Keys D = Keys.D;

        #region Keyboard Data

        /// <summary>
        /// The state of the keyboard as of the last update.
        /// </summary>
        private static KeyboardState currentKeyboardState;
       

        /// <summary>
        /// The state of the keyboard as of the last update.
        /// </summary>
        public static KeyboardState CurrentKeyboardState
        {
            get { return currentKeyboardState; }
        }

        /// <summary>
        /// The state of the keyboard as of the previous update.
        /// </summary>
        private static KeyboardState previousKeyboardState;


        /// <summary>
        /// Check if a key is pressed.
        /// </summary>
        public static bool IsKeyPressed(Keys key)
        {
            return currentKeyboardState.IsKeyDown(key);
        }


        /// <summary>
        /// Check if a key was just pressed in the most recent update.
        /// </summary>
        public static bool IsKeyTriggered(Keys key)
        {
            return (currentKeyboardState.IsKeyDown(key)) &&
                (!previousKeyboardState.IsKeyDown(key));
        }


        #endregion

        #region Mouse Data

        /// <summary>
        /// The state of the mouse as of the last update.
        /// </summary>
        private static MouseState currentMouseState;


        /// <summary>
        /// The state of the mouse as of the last update.
        /// </summary>
        public static MouseState CurrentMouseState
        {
            get { return currentMouseState; }
        }

        /// <summary>
        /// The state of the mouse as of the previous update.
        /// </summary>
        private static MouseState previousMouseState;

        /// <summary>
        /// Check if a key is pressed.
        /// </summary>
        public static MouseState PreviousMouseState
        {
            get { return previousMouseState; }
        }

        /// <summary>
        /// 
        /// </summary>
        public static Vector2 MousePosition
        {
            get { return new Vector2(currentMouseState.X, currentMouseState.Y); }
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static Boolean isLeftMouseButtonPressed()
        {
            return (currentMouseState.LeftButton == ButtonState.Pressed);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static Boolean isLeftMouseButtonTriggered()
        {
            return (isLeftMouseButtonPressed()
                && previousMouseState.LeftButton == ButtonState.Released);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static Boolean isRightMouseButtonPressed()
        {
            return (currentMouseState.RightButton == ButtonState.Pressed);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static Boolean isRightMouseButtonTriggered()
        {
            return (isRightMouseButtonPressed()
                && previousMouseState.RightButton == ButtonState.Released);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static Boolean isMiddleMouseButtonPressed()
        {
            return (currentMouseState.MiddleButton == ButtonState.Pressed);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static Boolean isMiddleMouseButtonTriggered()
        {
            return (isMiddleMouseButtonPressed()
                && previousMouseState.MiddleButton == ButtonState.Released);
        }

        #endregion

        #region Updating


        /// <summary>
        /// Updates the keyboard and gamepad control states.
        /// </summary>
        public static void Update()
        {
            // update the keyboard state
            previousKeyboardState = currentKeyboardState;
            currentKeyboardState = Keyboard.GetState();

            previousMouseState = currentMouseState;
            currentMouseState = Mouse.GetState();
        }

        #endregion
    }
}
